<?php
	class ModCharacter
	{
		public function index()
		{
			if( !$_REQUEST['name'] )
			{
				viewLayer::render( 'search_character' );
				return false;
			}
			
			$P = new OTS_Player;
			$P->find( $_REQUEST['name'] );
			
			if( $P->isLoaded() )
			{
				self::show( $P );
				return false;
			}
			
			viewLayer::render( NULL, 'Character <strong>' . $_REQUEST['name'] . '</strong> does not exist.' );
			viewLayer::render( 'search_character' );
			return false;
		}
		
		public function search()
		{
			$this->index();
		}
		
		public function show( $player = null )
		{
				$Player = new OTS_Player;
				if( !$player )
					$Player->load( intval($_GET['id']) );
				else
					$Player = $player;
					
				if( !$Player )
				{
					viewLayer::render( NULL, 'Player with specified ID does not exist.' );
					return false;
				}
				
				viewLayer::$var->player = $Player;
				viewLayer::$var->playeraac = dataLayer::$playersData[ intval($_GET['id']) ];
				
				POT::getInstance()->loadVocations( configParser::$confdir . configParser::getData('datadir') . 'XML/' . 'vocations.xml' );
				viewLayer::render('show_player');
		}
		
		public function delete()
		{
			if( !intval($_GET['id']) )
				viewLayer::render( NULL, "You have to specify player's ID." );
			
			$p = new OTS_Player;
			$p->load( intval( $_GET['id'] ) );
			if( !$p->isLoaded() )
				viewLayer::render( NULL, "Player with given ID does not exist." );
				
			if( $p->account->id === sessionLayer::getAccount() or sessionLayer::isAdministratorAccount() )
			{
				viewLayer::render( NULL, "Player " . $p->name . " is successfully deleted." );
				dataLayer::deletePlayerData( $p->id );
				$p->delete();
				
				return false;
			}
			else
			{
				viewLayer::render( NULL, "You have no permission to delete this character" );
				return false;
			}
		}
				
		
		public function edit()
		{
			if( !$_GET['id'] )
				return false;
			
			$Player = new OTS_Player;
			$Player->load( intval( $_GET['id'] ) );
			if( sessionLayer::getAccount() !== $Player->account->id and !sessionLayer::isAdministratorAccount() )
			{
				viewLayer::render( NULL, "You have no permission to edit this character" );
				return false;
			}
		
			if( $_POST['comment'] )
			{
				dataLayer::savePlayerData( intval($_GET['id']), dataLayer::$playersData[ intval($_GET['id']) ]['creationdate'], $_POST['comment'], intval($_POST['hiddeninfo']) );
				viewLayer::render( NULL, 'Player information has been successfully changed.<br /><a href="?m=account">[Back to an account page]</a>');
				return false;
			}
			
			if( !$Player )
			{
				viewLayer::render( NULL, 'Player with specified ID does not exist.' );
				return false;
			}	

			viewLayer::$var->player = $Player;
			viewLayer::$var->playeraac = dataLayer::$playersData[ intval($_GET['id']) ];
			viewLayer::render( 'edit_character' );	
			return true;
		
		}
	}
	

?>